<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\CreateConfigRequest;
class ConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public  function  siteconfig(){

        $config = DB::table('config')->where('name', 'siteconfig')->first();
        $config=json_decode($config->config,true);

        return  view("admin.config.siteconfig",compact('config'));
    
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateConfigRequest $request)
    {
       
       $data = $request->only(['name','title']);
       $data_config = $request->except(['name','title']);
       $data['config'] = json_encode($data_config);
       $data['created_at']=date("Y/m/d H:i:s",time());
       $data['updated_at']=date("Y/m/d H:i:s",time());
       $result = DB::table('config')->where('name','=',$request->name)->get();
        if(count($result)==0){
            $res = DB::table('config')->insert($data);
        }else{
            $res = DB::table('config')->where('name','=',$request->name)->update($data);
        }
        if($res===true || $res>0){
            session()->flash("data",['class'=>'success','msg'=>"更新成功"]);
        }else{
            session()->flash("data",['class'=>'danger','msg'=>"更新失败"]);
        }
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
